
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言指针 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part14/" />
    
    
    <link rel="prev" href="../part12/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../part9/">
            
                <a href="../part9/">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.14" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言指针</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x6307;&#x9488;">Go &#x8BED;&#x8A00;&#x6307;&#x9488;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x4E2D;&#x6307;&#x9488;&#x662F;&#x5F88;&#x5BB9;&#x6613;&#x5B66;&#x4E60;&#x7684;&#xFF0C;Go &#x8BED;&#x8A00;&#x4E2D;&#x4F7F;&#x7528;&#x6307;&#x9488;&#x53EF;&#x4EE5;&#x66F4;&#x7B80;&#x5355;&#x7684;&#x6267;&#x884C;&#x4E00;&#x4E9B;&#x4EFB;&#x52A1;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x63A5;&#x4E0B;&#x6765;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x4E00;&#x6B65;&#x6B65;&#x5B66;&#x4E60; Go &#x8BED;&#x8A00;&#x6307;&#x9488;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x6211;&#x4EEC;&#x90FD;&#x77E5;&#x9053;&#xFF0C;&#x53D8;&#x91CF;&#x662F;&#x4E00;&#x79CD;&#x4F7F;&#x7528;&#x65B9;&#x4FBF;&#x7684;&#x5360;&#x4F4D;&#x7B26;&#xFF0C;&#x7528;&#x4E8E;&#x5F15;&#x7528;&#x8BA1;&#x7B97;&#x673A;&#x5185;&#x5B58;&#x5730;&#x5740;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;Go &#x8BED;&#x8A00;&#x7684;&#x53D6;&#x5730;&#x5740;&#x7B26;&#x662F; &amp;&#xFF0C;&#x653E;&#x5230;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x524D;&#x4F7F;&#x7528;&#x5C31;&#x4F1A;&#x8FD4;&#x56DE;&#x76F8;&#x5E94;&#x53D8;&#x91CF;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x6F14;&#x793A;&#x4E86;&#x53D8;&#x91CF;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x5730;&#x5740;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

    fmt.Printf(<span class="hljs-string">&quot;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;: %x\n&quot;</span>, &amp;a  )
}
</code></pre>
<p>&#x6267;&#x884C;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;: c00012a008
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E86;&#x89E3;&#x4E86;&#x4EC0;&#x4E48;&#x662F;&#x5185;&#x5B58;&#x5730;&#x5740;&#x548C;&#x5982;&#x4F55;&#x53BB;&#x8BBF;&#x95EE;&#x5B83;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C06;&#x5177;&#x4F53;&#x4ECB;&#x7ECD;&#x6307;&#x9488;&#x3002;</p>
<hr>
<h2 id="&#x4EC0;&#x4E48;&#x662F;&#x6307;&#x9488;">&#x4EC0;&#x4E48;&#x662F;&#x6307;&#x9488;</h2>
<p>&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x4E86;&#x4E00;&#x4E2A;&#x503C;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;&#x3002;</p>
<p>&#x7C7B;&#x4F3C;&#x4E8E;&#x53D8;&#x91CF;&#x548C;&#x5E38;&#x91CF;&#xFF0C;&#x5728;&#x4F7F;&#x7528;&#x6307;&#x9488;&#x524D;&#x4F60;&#x9700;&#x8981;&#x58F0;&#x660E;&#x6307;&#x9488;&#x3002;&#x6307;&#x9488;&#x58F0;&#x660E;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> var_name *<span class="hljs-keyword">var</span>-<span class="hljs-keyword">type</span>
</code></pre>
<p><code>var-type</code> &#x4E3A;&#x6307;&#x9488;&#x7C7B;&#x578B;&#xFF0C;<code>var_name</code> &#x4E3A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x540D;&#xFF0C;<code>*</code> &#x53F7;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x53D8;&#x91CF;&#x662F;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x3002;&#x4EE5;&#x4E0B;&#x662F;&#x6709;&#x6548;&#x7684;&#x6307;&#x9488;&#x58F0;&#x660E;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> ip *<span class="hljs-keyword">int</span>        <span class="hljs-comment">/* &#x6307;&#x5411;&#x6574;&#x578B; */</span>
<span class="hljs-keyword">var</span> fp *<span class="hljs-keyword">float32</span>    <span class="hljs-comment">/* &#x6307;&#x5411;&#x6D6E;&#x70B9;&#x578B; */</span>
</code></pre>
<p>&#x672C;&#x4F8B;&#x4E2D;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x6307;&#x5411; int &#x548C; float32 &#x7684;&#x6307;&#x9488;&#x3002;</p>
<hr>
<h2 id="&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x6307;&#x9488;">&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x6307;&#x9488;</h2>
<p>&#x6307;&#x9488;&#x4F7F;&#x7528;&#x6D41;&#x7A0B;&#xFF1A;</p>
<ul>
<li>&#x5B9A;&#x4E49;&#x6307;&#x9488;&#x53D8;&#x91CF;&#xFF1B;</li>
<li>&#x4E3A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x8D4B;&#x503C;&#xFF1B;</li>
<li>&#x8BBF;&#x95EE;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x4E2D;&#x6307;&#x5411;&#x5730;&#x5740;&#x7684;&#x503C;&#x3002;</li>
</ul>
<p>&#x5728;&#x6307;&#x9488;&#x7C7B;&#x578B;&#x524D;&#x9762;&#x52A0;&#x4E0A; <code>*</code> &#x53F7;&#xFF08;&#x524D;&#x7F00;&#xFF09;&#x6765;&#x83B7;&#x53D6;&#x6307;&#x9488;&#x6240;&#x6307;&#x5411;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span>= <span class="hljs-number">20</span>   <span class="hljs-comment">/* &#x58F0;&#x660E;&#x5B9E;&#x9645;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> ip *<span class="hljs-keyword">int</span>        <span class="hljs-comment">/* &#x58F0;&#x660E;&#x6307;&#x9488;&#x53D8;&#x91CF; */</span>

    ip = &amp;a  <span class="hljs-comment">/* &#x6307;&#x9488;&#x53D8;&#x91CF;&#x7684;&#x5B58;&#x50A8;&#x5730;&#x5740; */</span>

    fmt.Printf(<span class="hljs-string">&quot;a &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;&#x662F;: %x\n&quot;</span>, &amp;a)

    <span class="hljs-comment">/* &#x6307;&#x9488;&#x53D8;&#x91CF;&#x7684;&#x5B58;&#x50A8;&#x5730;&#x5740; */</span>
    fmt.Printf(<span class="hljs-string">&quot;ip &#x53D8;&#x91CF;&#x50A8;&#x5B58;&#x7684;&#x6307;&#x9488;&#x5730;&#x5740;: %x\n&quot;</span>, ip)

    <span class="hljs-comment">/* &#x4F7F;&#x7528;&#x6307;&#x9488;&#x8BBF;&#x95EE;&#x503C; */</span>
    fmt.Printf(<span class="hljs-string">&quot;*ip &#x53D8;&#x91CF;&#x7684;&#x503C;: %d\n&quot;</span>, *ip)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;&#x662F;: c000136008
ip &#x53D8;&#x91CF;&#x50A8;&#x5B58;&#x7684;&#x6307;&#x9488;&#x5730;&#x5740;: c000136008
*ip &#x53D8;&#x91CF;&#x7684;&#x503C;: 20
</code></pre>
<hr>
<h2 id="go-&#x7A7A;&#x6307;&#x9488;">Go &#x7A7A;&#x6307;&#x9488;</h2>
<p>&#x5F53;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x88AB;&#x5B9A;&#x4E49;&#x540E;&#x6CA1;&#x6709;&#x5206;&#x914D;&#x5230;&#x4EFB;&#x4F55;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x5B83;&#x7684;&#x503C;&#x4E3A; <code>nil</code>&#x3002;</p>
<p><code>nil</code> &#x6307;&#x9488;&#x4E5F;&#x79F0;&#x4E3A;&#x7A7A;&#x6307;&#x9488;&#x3002;</p>
<p><code>nil</code>&#x5728;&#x6982;&#x5FF5;&#x4E0A;&#x548C;&#x5176;&#x5B83;&#x8BED;&#x8A00;&#x7684;<code>null&#x3001;None&#x3001;nil&#x3001;NULL</code>&#x4E00;&#x6837;&#xFF0C;&#x90FD;&#x6307;&#x4EE3;&#x96F6;&#x503C;&#x6216;&#x7A7A;&#x503C;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x901A;&#x5E38;&#x7F29;&#x5199;&#x4E3A; <code>ptr</code>&#x3002;</p>
<p>&#x67E5;&#x770B;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span>  ptr *<span class="hljs-keyword">int</span>

    fmt.Printf(<span class="hljs-string">&quot;ptr &#x7684;&#x503C;&#x4E3A; : %x\n&quot;</span>, ptr)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">ptr &#x7684;&#x503C;&#x4E3A; : 0
</code></pre>
<p>&#x7A7A;&#x6307;&#x9488;&#x5224;&#x65AD;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> ptr != <span class="hljs-literal">nil</span>     <span class="hljs-comment">/* ptr &#x4E0D;&#x662F;&#x7A7A;&#x6307;&#x9488; */</span>
<span class="hljs-keyword">if</span> ptr == <span class="hljs-literal">nil</span>    <span class="hljs-comment">/* ptr &#x662F;&#x7A7A;&#x6307;&#x9488; */</span>
</code></pre>
<hr>
<h2 id="go&#x6307;&#x9488;&#x66F4;&#x591A;&#x5185;&#x5BB9;">Go&#x6307;&#x9488;&#x66F4;&#x591A;&#x5185;&#x5BB9;</h2>
<p>&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x5C06;&#x4E3A;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;Go&#x8BED;&#x8A00;&#x4E2D;&#x66F4;&#x591A;&#x7684;&#x6307;&#x9488;&#x5E94;&#x7528;&#xFF1A;</p>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x5185;&#x5BB9;</font></th>
        <th bgcolor="#4b4b4b" width="100%"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">Go &#x6307;&#x9488;&#x6570;&#x7EC4;</td>
        <td align="center" bgcolor="#f5f2ee">&#x4F60;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x6570;&#x7EC4;&#x6765;&#x5B58;&#x50A8;&#x5730;&#x5740;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">Go &#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;</td>
        <td align="center" bgcolor="#ffffff">Go &#x652F;&#x6301;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">Go &#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x6307;&#x9488;&#x53C2;&#x6570;</td>
        <td align="center" bgcolor="#f5f2ee">&#x901A;&#x8FC7;&#x5F15;&#x7528;&#x6216;&#x5730;&#x5740;&#x4F20;&#x53C2;&#xFF0C;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x65F6;&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x5176;&#x503C;</td>
    </tr>
</table>

<h3 id="go-&#x6307;&#x9488;&#x6570;&#x7EC4;">Go &#x6307;&#x9488;&#x6570;&#x7EC4;</h3>
<p>&#x5728;&#x6211;&#x4EEC;&#x4E86;&#x89E3;&#x6307;&#x9488;&#x6570;&#x7EC4;&#x524D;&#xFF0C;&#x5148;&#x770B;&#x4E2A;&#x5B9E;&#x4F8B;&#xFF0C;&#x5B9A;&#x4E49;&#x4E86;&#x957F;&#x5EA6;&#x4E3A; 3 &#x7684;&#x6574;&#x578B;&#x6570;&#x7EC4;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">const</span> MAX <span class="hljs-keyword">int</span> = <span class="hljs-number">3</span>

<span class="hljs-keyword">func</span> main() {
    a := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">10</span>,<span class="hljs-number">100</span>,<span class="hljs-number">200</span>}
    <span class="hljs-keyword">var</span> i <span class="hljs-keyword">int</span>

    <span class="hljs-keyword">for</span> i = <span class="hljs-number">0</span>; i &lt; MAX; i++ {
        fmt.Printf(<span class="hljs-string">&quot;a[%d] = %d\n&quot;</span>, i, a[i] )
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a[0] = 10
a[1] = 100
a[2] = 200
</code></pre>
<p>&#x6709;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x4FDD;&#x5B58;&#x6570;&#x7EC4;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x5C31;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x5230;&#x6307;&#x9488;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x58F0;&#x660E;&#x4E86;&#x6574;&#x578B;&#x6307;&#x9488;&#x6570;&#x7EC4;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> ptr [MAX] *<span class="hljs-keyword">int</span>
</code></pre>
<p>ptr &#x4E3A;&#x6574;&#x578B;&#x6307;&#x9488;&#x6570;&#x7EC4;&#x3002;&#x56E0;&#x6B64;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x90FD;&#x6307;&#x5411;&#x4E86;&#x4E00;&#x4E2A;&#x503C;&#x3002;&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x7684;&#x4E09;&#x4E2A;&#x6574;&#x6570;&#x5C06;&#x5B58;&#x50A8;&#x5728;&#x6307;&#x9488;&#x6570;&#x7EC4;&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">const</span> MAX <span class="hljs-keyword">int</span> = <span class="hljs-number">3</span>

<span class="hljs-keyword">func</span> main() {
    a := [] <span class="hljs-keyword">int</span> {<span class="hljs-number">10</span>,<span class="hljs-number">100</span>,<span class="hljs-number">200</span>}
    <span class="hljs-keyword">var</span> i <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">var</span> ptr [MAX] *<span class="hljs-keyword">int</span>

    <span class="hljs-keyword">for</span>  i = <span class="hljs-number">0</span>; i &lt; MAX; i++ {
        ptr[i] = &amp;a[i]    <span class="hljs-comment">/* &#x6574;&#x6570;&#x5730;&#x5740;&#x8D4B;&#x503C;&#x7ED9;&#x6307;&#x9488;&#x6570;&#x7EC4; */</span>
    }

    <span class="hljs-keyword">for</span>  i = <span class="hljs-number">0</span>; i &lt; MAX; i++ {
        fmt.Printf(<span class="hljs-string">&quot;a[%d] = %d\n&quot;</span>, i, *ptr[i] )
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a[0] = 10
a[1] = 100
a[2] = 200
</code></pre>
<ol>
<li><p>&#x6307;&#x9488;&#x6570;&#x7EC4;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F7F;&#x7528; <strong><code>range</code></strong> &#x5FAA;&#x73AF;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">const</span> max = <span class="hljs-number">3</span>

 <span class="hljs-keyword">func</span> main() {
     number := [max] <span class="hljs-keyword">int</span> {<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>}
     <span class="hljs-keyword">var</span> ptrs [max] *<span class="hljs-keyword">int</span>        <span class="hljs-comment">// &#x6307;&#x9488;&#x6570;&#x7EC4;</span>
     <span class="hljs-comment">// &#x5C06;number&#x6570;&#x7EC4;&#x7684;&#x503C;&#x7684;&#x5730;&#x5740;&#x8D4B;&#x7ED9;ptrs</span>
     <span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> number {
         ptrs[i] = &amp;number[i]
     }

     <span class="hljs-keyword">for</span> i, x := <span class="hljs-keyword">range</span> ptrs {
         fmt.Printf(<span class="hljs-string">&quot;&#x6307;&#x9488;&#x6570;&#x7EC4;&#xFF1A;&#x7D22;&#x5F15;:%d &#x503C;:%d &#x503C;&#x7684;&#x5185;&#x5B58;&#x5730;&#x5740;:%d\n&quot;</span>, i, *x, x)
     }
 }
</code></pre>
</li>
</ol>
<h3 id="go-&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;">Go &#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;</h3>
<p>&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x5B58;&#x653E;&#x7684;&#x53C8;&#x662F;&#x53E6;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;&#xFF0C;&#x5219;&#x79F0;&#x8FD9;&#x4E2A;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x4E3A;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x3002;</p>
<p>&#x5F53;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x6307;&#x9488;&#x5B58;&#x653E;&#x7B2C;&#x4E8C;&#x4E2A;&#x6307;&#x9488;&#x7684;&#x5730;&#x5740;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6307;&#x9488;&#x5B58;&#x653E;&#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part13/01.jpg">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;"></div>
</center>

<p>&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x58F0;&#x660E;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">var</span> ptr **<span class="hljs-keyword">int</span>
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x4E3A;&#x6574;&#x578B;&#x3002;</p>
<p>&#x8BBF;&#x95EE;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x503C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x4E24;&#x4E2A; <code>*</code> &#x53F7;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">var</span> ptr *<span class="hljs-keyword">int</span>
    <span class="hljs-keyword">var</span> pptr **<span class="hljs-keyword">int</span>

    a = <span class="hljs-number">3000</span>

    <span class="hljs-comment">/* &#x6307;&#x9488; ptr &#x5730;&#x5740; */</span>
    ptr = &amp;a

    <span class="hljs-comment">/* &#x6307;&#x5411;&#x6307;&#x9488; ptr &#x5730;&#x5740; */</span>
    pptr = &amp;ptr

    <span class="hljs-comment">/* &#x83B7;&#x53D6; pptr &#x7684;&#x503C; */</span>
    fmt.Printf(<span class="hljs-string">&quot;&#x53D8;&#x91CF; a = %d\n&quot;</span>, a)
    fmt.Printf(<span class="hljs-string">&quot;&#x6307;&#x9488;&#x53D8;&#x91CF; *ptr = %d\n&quot;</span>, *ptr)
    fmt.Printf(<span class="hljs-string">&quot;&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF; **pptr = %d\n&quot;</span>, **pptr)
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x6267;&#x884C;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x53D8;&#x91CF; a = 3000
&#x6307;&#x9488;&#x53D8;&#x91CF; *ptr = 3000
&#x6307;&#x5411;&#x6307;&#x9488;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF; **pptr = 3000
</code></pre>
<h3 id="go-&#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x6307;&#x9488;&#x53C2;&#x6570;">Go &#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x6307;&#x9488;&#x53C2;&#x6570;</h3>
<p>Go &#x8BED;&#x8A00;&#x5141;&#x8BB8;&#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x6307;&#x9488;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5728;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x7684;&#x53C2;&#x6570;&#x4E0A;&#x8BBE;&#x7F6E;&#x4E3A;&#x6307;&#x9488;&#x7C7B;&#x578B;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x4EE5;&#x4E0B;&#x5B9E;&#x4F8B;&#x6F14;&#x793A;&#x4E86;&#x5982;&#x4F55;&#x5411;&#x51FD;&#x6570;&#x4F20;&#x9012;&#x6307;&#x9488;&#xFF0C;&#x5E76;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x540E;&#x4FEE;&#x6539;&#x51FD;&#x6570;&#x5185;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D; a &#x7684;&#x503C; : %d\n&quot;</span>, a )
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x524D; b &#x7684;&#x503C; : %d\n&quot;</span>, b )

    <span class="hljs-comment">/* &#x8C03;&#x7528;&#x51FD;&#x6570;&#x7528;&#x4E8E;&#x4EA4;&#x6362;&#x503C;
        * &amp;a &#x6307;&#x5411; a &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;
        * &amp;b &#x6307;&#x5411; b &#x53D8;&#x91CF;&#x7684;&#x5730;&#x5740;
        */</span>
    swap(&amp;a, &amp;b)

    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : %d\n&quot;</span>, a )
    fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : %d\n&quot;</span>, b )
}

<span class="hljs-keyword">func</span> swap(x *<span class="hljs-keyword">int</span>, y *<span class="hljs-keyword">int</span>) {
    <span class="hljs-keyword">var</span> temp <span class="hljs-keyword">int</span>
    temp = *x    <span class="hljs-comment">/* &#x4FDD;&#x5B58; x &#x5730;&#x5740;&#x7684;&#x503C; */</span>
    *x = *y      <span class="hljs-comment">/* &#x5C06; y &#x8D4B;&#x503C;&#x7ED9; x */</span>
    *y = temp    <span class="hljs-comment">/* &#x5C06; temp &#x8D4B;&#x503C;&#x7ED9; y */</span>
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x5B9E;&#x4F8B;&#x8F93;&#x51FA;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x4EA4;&#x6362;&#x524D; a &#x7684;&#x503C; : 100
&#x4EA4;&#x6362;&#x524D; b &#x7684;&#x503C; : 200
&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : 200
&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : 100
</code></pre>
<hr>
<h2 id="ps">PS</h2>
<ol>
<li><p>&#x4E00;&#x4E2A;&#x66F4;&#x7B80;&#x6D01;&#x7684;&#x53D8;&#x91CF;&#x4EA4;&#x6362;&#x5B9E;&#x4F8B;1&#xFF1A;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
     <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
     <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>
     swap(&amp;a, &amp;b)

     fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : %d\n&quot;</span>, a )
     fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : %d\n&quot;</span>, b )
 }

 <span class="hljs-comment">/* &#x4EA4;&#x6362;&#x51FD;&#x6570;&#x8FD9;&#x6837;&#x5199;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#xFF0C;&#x4E5F;&#x662F; go &#x8BED;&#x8A00;&#x7684;&#x7279;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x4E0B;&#xFF0C;c++ &#x548C; c# &#x662F;&#x4E0D;&#x80FD;&#x8FD9;&#x4E48;&#x5E72;&#x7684; */</span>
 <span class="hljs-keyword">func</span> swap(x *<span class="hljs-keyword">int</span>, y *<span class="hljs-keyword">int</span>){
     *x, *y = *y, *x
 }
</code></pre>
</li>
<li><p>&#x4E00;&#x4E2A;&#x66F4;&#x7B80;&#x6D01;&#x7684;&#x53D8;&#x91CF;&#x4EA4;&#x6362;&#x5B9E;&#x4F8B;2&#xFF1A;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
     <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
     <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span>= <span class="hljs-number">200</span>
     a, b = b, a

     fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; a &#x7684;&#x503C; : %d\n&quot;</span>, a )
     fmt.Printf(<span class="hljs-string">&quot;&#x4EA4;&#x6362;&#x540E; b &#x7684;&#x503C; : %d\n&quot;</span>, b )
 }
</code></pre>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part12/" class="navigation navigation-prev " aria-label="Previous page: Go 语言数组">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part14/" class="navigation navigation-next " aria-label="Next page: Go 语言结构体">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言指针","level":"1.14","depth":1,"next":{"title":"Go 语言结构体","level":"1.15","depth":1,"path":"part14/README.md","ref":"part14/README.md","articles":[]},"previous":{"title":"Go 语言数组","level":"1.13","depth":1,"path":"part12/README.md","ref":"part12/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part13/README.md","mtime":"2021-01-20T01:48:29.760Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

